﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HR60_PanelSoft.NetWork;
using HR60_PanelSoft.NetWork.Dto;

namespace HR60_PanelSoft.View
{
    //GC: [g1]设置窗口[WindowSetting]
    //GC: [g2]联调7-24-2：新需求，网关设置更改到网关扫描位置[WindowSetting]


    /*
     * 1.如果网关号为空，表示设置面板
     * 2.如果网关号不为空，表示设置网关
     * 3.门牌、面板、单元号、网络号共同决定一个zigbee地址；
     */
	public partial class WindowSetting : Form
	{
        
        /* [g1.s1] */

        public GateWayDto GWD = new GateWayDto();

        private NetService _service = new NetService();

        private NetWork.NetService _netS = new NetService();
        /* end of [g1.s1] */
        
        /* [g2.s3] */
        private bool _IsCanQuit = true;

        /* end of [g2.s3] */
        
        /* [g2.s6]成功标志 */
        private bool _isSuccessed = false;
        /// <summary>
        /// 调用者，需要根据此标志，来判断是否设置成功
        /// </summary>
        public bool IsSuccessed
        {
            get
            {
                return _isSuccessed;
            }
        }

        /* end of [g2.s6] */


		public WindowSetting(GateWayDto gw)
		{
			InitializeComponent();
            /* [g2.s5] */
            this.FormClosing += SettingClosingEventHandler;
            /* end of [g2.s5] */

            this.GWD = gw;
		}

        private void BTN_ONMAIN_SEND_Click(object sender, EventArgs e)
        {
            //_netS.setGateWay(CurGW,null);
            try
            {
                /* [g2.s1]设置网关 */
                foreach (Control c in this.Controls)
                {
                    if (!(c is TextBox))
                        continue;
                    if (!AssertString2Int(((TextBox)c).Text))
                    {
                        View.ViewUtils.showAlert("请填写整数！");
                        _IsCanQuit = false;
                    }
                }

                GWD.MenPaiNo = this.TBOX_ONMAIN_DOOR.Text;
                GWD.PanelNo = this.TBOX_ONMAIN_PANEL.Text;
                GWD.DuanYuanNo = this.TBOX_ONMAIN_UNIT.Text;
                GWD.NetNo = this.TBOX_ONMAIN_NET.Text;
                GWD.SubNo = this.TBOX_ONMAIN_GWIDX.Text;

                /* end of [g2.s1] */
                _netS.setGateWay(GWD);


            }
            catch (Exception ee)
            {
                _isSuccessed = false;
                View.ViewUtils.showError( "设置网关失败: \r\n" + ee.Message);
                return;
            }
            finally
            {
                _isSuccessed = false;
            }
            _isSuccessed = true;

        }



        private void WindowSetting_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                this.TBOX_ONMAIN_DOOR.Text = GWD.MenPaiNo;
                this.TBOX_ONMAIN_PANEL.Text = GWD.PanelNo;
                this.TBOX_ONMAIN_UNIT.Text = GWD.DuanYuanNo;
                this.TBOX_ONMAIN_NET.Text = GWD.NetNo;
                this.TBOX_ONMAIN_GWIDX.Text = GWD.SubNo;

                /*
                 * 如果网关号为空，表示设置面板
                 * 如果网关号不为空，表示设置网关
                 * 门牌、网关、单元号、网络号共同决定一个zigbee地址；
                 */
            }
        }

        /* [g2.s2] */
        private bool AssertString2Int(string s)
        {
            int tempN = -1;
            return int.TryParse(s, out tempN);
        }
        /* end of [g2.s2] */

        /* [g2.s4] */
        private void SettingClosingEventHandler(object sender, FormClosingEventArgs e)
        {
            e.Cancel = !_IsCanQuit;
        }

        /* end of [g2.s4] */


	}
}
